// 引入错误处理
var createError = require('http-errors');
// 引入express框架
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
// 访问日志
var logger = require('morgan');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
// 引入班级路由
var classRouter = require("./routes/class");
// 引入学生路由
var studentRouter = require("./routes/student");

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// app.use(logger('dev'));
// 解析json
app.use(express.json());
// 解析请求体
app.use(express.urlencoded({ extended: false }));
// 使用cookiej解析
app.use(cookieParser());
// 静态资源托管
app.use(express.static(path.join(__dirname, 'public')));

// 路由模块
// app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/class', classRouter);
app.use('/student', studentRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
